package org.blame.lotterysystem.controller.handler;


import org.blame.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import org.blame.lotterysystem.common.exception.ControllerException;
import org.blame.lotterysystem.common.exception.ServiceException;
import org.blame.lotterysystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger= LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(ServiceException.class)
    public CommonResult<?> serviceException(ServiceException serviceException){
        logger.error("serviceException:",serviceException);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),serviceException.getMessage());

    }

    @ExceptionHandler(ControllerException.class)
    public CommonResult<?> controllerException(ControllerException controllerException){
        logger.error("controllerException:",controllerException);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),controllerException.getMessage());

    }

    @ExceptionHandler(Exception.class)
    public CommonResult<?> exception(Exception exception){
        logger.error("服务异常:",exception);

        return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),exception.getMessage());

    }
}
